// antd 按需加载
import { Toast, Portal } from '@ant-design/react-native';
import { Actions } from 'react-native-router-flux';

import store from '../../../store/store.js'
import * as ActionTypes from '../../../constants/constants.js'
import { isUser, isWxUser, path, saveData, loadData, removeData, requstPromise } from '../../../util/utils.js'
import request from '../../../api/request.js'

export async function getCountry(options) {
    const { dispatch } = this.props
    const res = await request.MemberController.country(options)
    if (res.error) {
        dispatch({
            type: ActionTypes.countryError,
            data: res
        })
        return
    }
    res.data.data.map(item => {
        item.label = item.stateCn
        item.value = item.id
    })
    dispatch({
        type: ActionTypes.country,
        data: res.data.data
    })
}

export async function getAddress(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.address(options)
    Portal.remove(loading)
    return requstPromise(res)
}

export async function addAddress(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.add(options)
    Portal.remove(loading)
    if (res.data.code == 200) {
        Toast.success('添加成功', 1, null, false)
        this.getAddressList()
        Actions.pop()
    } else {
        Toast.fail('添加失败', 1, null, false)
    }
}

export async function editAddress(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.update(options)
    Portal.remove(loading)
    if (res.data.code == 200) {
        Toast.success('修改成功', 1, null, false)
        this.getAddressList()
        Actions.pop()
    } else {
        Toast.fail('修改失败', 1, null, false)
    }
}
